package com.ruoyi.biz.letter.controller;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.letter.export.BizLeaderMailboxExport;
import com.ruoyi.biz.letter.export.BizProvinceLetterExport;
import com.ruoyi.biz.letter.util.AcceptDeptUtil;
import com.ruoyi.biz.letter.util.CategoryUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.letter.domain.BizProvinceLetter;
import com.ruoyi.biz.letter.service.IBizProvinceLetterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 省级信访Controller
 *
 * @author Fei.Cong
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/province/letter")
public class BizProvinceLetterController extends BaseController {

    @Autowired
    private IBizProvinceLetterService bizProvinceLetterService;

    /**
     * 查询省级信访列表
     */
    @PreAuthorize("@ss.hasPermi('province:letter:list')")
    @GetMapping("/list")
    public AjaxResult list(BizProvinceLetter bizProvinceLetter) {
        LambdaQueryWrapper<BizProvinceLetter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizProvinceLetterService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询省级信访列表
     */
    @PreAuthorize("@ss.hasPermi('province:letter:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizProvinceLetter bizProvinceLetter) {
        CategoryUtil.handleQueryCategoryId(bizProvinceLetter);
        return getDataTable(bizProvinceLetterService.pageList(bizProvinceLetter, startPage()));
    }

    /**
     * 导出省级信访列表
     */
    @PreAuthorize("@ss.hasPermi('province:letter:export')")
    @Log(title = "省级信访", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProvinceLetter bizProvinceLetter) {
        List<BizProvinceLetter> list = bizProvinceLetterService.queryList(bizProvinceLetter);
        List<BizProvinceLetterExport> exportList = list.stream().map(v -> {
            BizProvinceLetterExport export = new BizProvinceLetterExport();
            BeanUtils.copyProperties(v, export);
            export.setFirstCategoryName(Optional.ofNullable(v.getPCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            export.setSecondCategoryName(Optional.ofNullable(v.getSCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            return export;
        }).collect(Collectors.toList());
        ExcelUtil<BizProvinceLetterExport> util = new ExcelUtil<BizProvinceLetterExport>(BizProvinceLetterExport.class);
        util.exportEasyExcel(response, exportList, "省级信访数据");
    }

    /**
     * 获取省级信访详细信息
     */
    @PreAuthorize("@ss.hasPermi('province:letter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BizProvinceLetter bizProvinceLetter = bizProvinceLetterService.getById(id);
        CategoryUtil.getCategoryIds(bizProvinceLetter);
        AcceptDeptUtil.getAcceptDept(bizProvinceLetter);
        return success(bizProvinceLetter);
    }

    /**
     * 新增省级信访
     */
    @PreAuthorize("@ss.hasPermi('province:letter:add')")
    @Log(title = "省级信访", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProvinceLetter bizProvinceLetter) {
        return toAjax(bizProvinceLetterService.save(bizProvinceLetter));
    }

    /**
     * 修改省级信访
     */
    @PreAuthorize("@ss.hasPermi('province:letter:edit')")
    @Log(title = "省级信访", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProvinceLetter bizProvinceLetter) {
        CategoryUtil.buildCategoryId(bizProvinceLetter);
        AcceptDeptUtil.buildAcceptDept(bizProvinceLetter);
        return toAjax(bizProvinceLetterService.updateById(bizProvinceLetter));
    }

    /**
     * 删除省级信访
     */
    @PreAuthorize("@ss.hasPermi('province:letter:remove')")
    @Log(title = "省级信访", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizProvinceLetterService.removeBatchByIds(ids));
    }
}
